﻿using System;
using System.Collections.Generic;
using System.Text;
using G3.State;
using G3.AI;

namespace G3.Controller
{
    class Controller
    {
        static private Controller onlyInstance = null;

        private G3.State.State myState;
        private G3.State.Car myCar;
        private MainWindow myMainWindow;
        private DateTime lastChecked;

        public G3.State.State AttachedState
        {
            get
            {
                return myState;
            }
        }

        private Controller()
        {
            myState = new G3.State.State();
            //myState.Init();
            myCar = new Car();
        }

        static public Controller GetOnlyInstance()
        {
            if (onlyInstance == null)
                onlyInstance = new Controller();
            return onlyInstance;
        }

        public void SetGraphicPanel(MainWindow myMainWindow)
        {
            this.myMainWindow = myMainWindow;
        }

        public void SetWheelThrootlePositions(double throotle, double wheel)
        {
            myState.Throttle = throotle;
            myState.Wheel = wheel;
            if(myMainWindow!=null)
            {
                myMainWindow.SetAccelerator((int)throotle);
                myMainWindow.SetWheel((int)wheel);
            }
        }

        public void SetStart()
        {
            //lastChecked = null;
            //TODO: időben meghívni
        }

        public KeyValuePair<Pair,double> GetCarPositionAndAngle()
        {
            DateTime now = DateTime.Now;
            TimeSpan diff;
            if (lastChecked == new DateTime())
                diff = new TimeSpan(0);
            else
                diff = (now - lastChecked);

            lastChecked = now;
            myState.Simulate(diff.TotalMilliseconds);
            Pair cP = myState.CarPosition;
            cP.y *= (-1);
            return new KeyValuePair<Pair, double>(cP, Math.PI / 2 + myState.CarOrientationRadian);
        }

        public double GetCarSpeed()
        {
            return myState.CarSpeed * 3.6;
        }

        public void MapLoaded()
        {
            myState.Init();
        }

        public void Reset()
        {
            myCar = new Car();
            myState = new G3.State.State();
            myState.Init();
        }

        public void ShowOnMap()
        {
            myMainWindow.ShowGraphics();
        }
    }
}
